;;(load "cyl.scm")
;;(load "tran-s.scm")
;;(load "scm.scm")
;;(load "sys-scm.scm")
;;(load "test-ut.scm")
(define (rd-file-string fn)
  (let ((p (open-input-file fn))
	)
    (get-string-all p)))
(define (wr-file-lists fn l)
  (let ((p (open-output-file fn 'replace)))
    (let f ((ls l))
      (if (not (null? ls))
	  (begin
	    (write (car ls) p)
	    (newline p)
	    (f (cdr ls)))))
    (close-port p)))
(define (wr-file-list fn l)
  (let ((p (open-output-file fn 'replace)))
    (write l p)
    (close-port p)))
(define (cyl->file fn . flag)
  (let ((p (open-output-file (if (null? flag) (format "./test/~a.cyl" fn) (format "~a.cyl" fn)) 'replace))
	(q (open-output-file (if (null? flag) (format "./test/~a.cys" fn) (format "~a.cys" fn)) 'replace))
	(ss (if (null? flag) (file->string fn)  (file->string fn flag)))
	;;(r (open-output-file (format "/home/y/proj/ts/~a.scm" fn) 'replace))
	(l '())
	(ll '())
	(s "")
	)
    (set! cyl-undefined-symbol-list '())
    (set! cyl-local-symbol-backup-list '())
    (set! cyl-current-symbol-dict-list '())
    (set! l (cyl ss 0))
    (set! s (nth 0 l))
    (set! l (nth 1 l))
    (pretty-print 符号位置序列号 p)
    (pretty-print l p)
    ;;(write "\n\n" p)
    (pretty-print cyl-local-symbol-backup-list p)
    ;;(write "\n" p)
    (pretty-print cyl-current-symbol-dict-list p)
    ;;(write "\n" p)
    (pretty-print cyl-reg-list p)
    (pretty-print cyl-undefined-symbol-list p)
    (pretty-print cyl-error-warning-list p)
    (close-port p)
    ;;(pretty-print s q)
    ;;(close-port q)
    (set! ll (gen-sym-list l))
    (set! l (end-rew-cyl (end-seq ll) l))
    (pretty-print 符号位置序列号 q)
    (pretty-print l q)
    (pretty-print cyl-local-symbol-backup-list q)
    (pretty-print cyl-current-symbol-dict-list q)
    (pretty-print cyl-reg-list q)
    (pretty-print cyl-undefined-symbol-list q)
    (pretty-print cyl-error-warning-list q)
    (pretty-print ll q)
    (close-port q)
    ;;(pretty-print (list->cfg cyl-local-symbol-backup-list) r)
    ;;(pretty-print (list->cfg cyl-current-symbol-dict-list) r)
    ;;(set! l (cyl->scm l))
    ;;(close-port r)
    ;;(y-or-n "fn=~a\n" fn)
    (if (null? flag)
	(begin
	  (string->file (format "~a.cyt" fn) s)
	  (set! s (cyl->lisp l 'scm))
	  (string->file (format "~a.scm" fn) s)
	  )
	(begin
	  (string->file (format "~a.cyt" fn) s #t)
	  (set! s (cyl->lisp l 'scm))
	  (string->file (format "~a.scm" fn) s #t)
	  ))
    ))
(define (cyl->file/el fn)
  (let ((p (open-output-file (format "./test/~a.cyl" fn) 'replace))
	(q (open-output-file (format "./test/~a.cyt" fn) 'replace))
	(l '())
	(s "")
	)
    (set! cyl-undefined-symbol-list '())
    (set! cyl-local-symbol-backup-list '())
    (set! cyl-current-symbol-dict-list '())
    (set! l (cyl (file->string fn) 0))
    (set! s (nth 0 l))
    (set! l (nth 1 l))
    (pretty-print 符号位置序列号 p)
    (pretty-print l p)
    ;;(write "\n\n" p)
    (pretty-print cyl-local-symbol-backup-list p)
    ;;(write "\n" p)
    (pretty-print cyl-current-symbol-dict-list p)
    ;;(write "\n" p)
    (pretty-print cyl-undefined-symbol-list p)
    (pretty-print cyl-error-warning-list p)
    (close-port p)
    (pretty-print s q)
    (close-port q)
    (string->file (format "~a.cyt" fn) s)
    (set! s (cyl->lisp l 'el))
    (string->file (format "~a.el" fn) s)
    ))
(define (list->file fn l . flag)
  (let ((p (open-output-file (if (null? flag) (format "./test/~a" fn) fn) 'replace)))
   	(pretty-print l p)
    (close-port p)))
(define (list-->file fn l . flag)
  (let ((p (open-output-file (if (null? flag) (format "./test/~a" fn) fn) 'append)))
    (write l p)
    (close-port p)))
(define (file->string fn . flag)
   (let ((p (open-input-file (if (null? flag) (format "./test/~a.cy" fn) (format "~a.cy" fn))))
	)
     (get-string-all p)))
(define (string->file fn s . flag)
   (let ((p (open-output-file (if (null? flag) (format "./test/~a" fn) fn) 'replace)))
     (tr-string (i c s 0)
		(put-char p c))
     (close-port p)))
(define (cy-eval s)
  (let ((l (cadr (cyl s 0))))
    (let ((p (open-input-string (cyl->scm l))))
      (eval (read p)))))
(define (read-string-from-cyfile fn . flag)
  (let ((p (open-input-file (if (null? flag) (format "./test/~a.cy" fn) (format "~a.cy" fn)))))
    (let ((cyl-str (get-line p))
	  (l '())
	  (cyl-l '())
	  (scm-s "")
	  ;;(scm-l '())
	  (ss "")
	  (s ""))
      (loop
       (if (port-eof? p) (breakout)
	   (begin
	     (set! s (get-line p))
	     ;;(y-or-n "000:s=~S,cyl-str=~S\n" s cyl-str)
	     (if (string=? s "")
		 (if (> (string-length cyl-str) 0)
		     (begin
		       (y-or-n "read-string-from-cyfile:0:cyl-str=~S,len=~a\n" cyl-str (string-length cyl-str))
		       (set! l (cyl cyl-str 0))
		       (set! scm-s (string-append scm-s (cyl->scm (nth 1 l))))
		       (set! ss (string-append ss (nth 0 l)))
		       (set! cyl-l (append cyl-l (nth 1 l)))
		       (set! cyl-str "")
		       ))
		 (if (rd-r-deli (string-ref s 0))
		     (begin
		       (set! cyl-str (string-append cyl-str "\n" s))
		       ;;(y-or-n "111:s=~S,cyl-str=~S\n" s cyl-str)
		       )
		     (if (> (string-length cyl-str) 0)
			 (begin
			   (y-or-n "read-string-from-cyfile:2:cyl-str=~S,len=~a\n" cyl-str (string-length cyl-str))
			   (set! l (cyl cyl-str 0))
			   ;;(y-or-n "l=~a\n" l)
			   (set! scm-s (string-append scm-s (cyl->scm (nth 1 l))))
			   (set! ss (string-append ss (nth 0 l)))
			   (set! cyl-l (append cyl-l (nth 1 l)))
			   (set! cyl-str s))
			 (set! cyl-str s)
			 )
		     ))
	     )
	   )
       )
      (if (> (string-length cyl-str) 0)
	  (begin
	(y-or-n "read-string-from-cyfile:3:cyl-str=~S,len=~a\n" cyl-str (string-length cyl-str))
	(set! l (cyl cyl-str 0))
	(set! scm-s (string-append scm-s (cyl->scm (nth 1 l))))
	(set! ss (string-append ss (nth 0 l)))
	(set! cyl-l (append cyl-l (nth 1 l)))
	))
      (let ((p (open-output-file (if (null? flag) (format "./test/~a.cyl" fn) (format "~a.cyl" fn)) 'replace))
	    ;;(q (open-output-file (if (null? flag) (format "./test/~a.cyt" fn) (format "~a.cyt" fn)) 'replace))
	    )
	(pretty-print 符号位置序列号 p)
	(pretty-print cyl-l p)
        (pretty-print cyl-local-symbol-backup-list p)
        (pretty-print cyl-current-symbol-dict-list p)
        (pretty-print cyl-reg-list p)
	(pretty-print cyl-undefined-symbol-list p)
	(pretty-print cyl-error-warning-list p)
	(close-port p)
	;;(pretty-print ss q)
	;;(close-port q)
	(string->file (format "~a.cyt" fn) ss flag)
	(string->file (format "~a.scm" fn) scm-s flag)
	(set! cyl-local-symbol-backup-list '())
	(set! cyl-current-symbol-dict-list '())
	(set! cyl-reg-list '())
	(set! cyl-undefined-symbol-list '())
	(set! cyl-error-waruing-list '())
	)
      (list ss cyl-l scm-s)
      )
    )
)
